package com.isyscore.handler;

import com.isyscore.config.JWTConfig;
import com.isyscore.entity.SelfUserEntity;
import com.isyscore.util.JWTTokenUtil;
import com.isyscore.util.ResultUtil;
import com.isyscore.vo.ResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: liuxq@isyscore.com
 * @Date: 2021-04-16 09:51:48
 * @Description: 登录成功处理类
 */
@Slf4j
@Component
public class UserLoginSuccessHandler implements AuthenticationSuccessHandler {
    /**
     * 登录成功返回结果
     */
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        // 组装JWT
        SelfUserEntity selfUserEntity = (SelfUserEntity) authentication.getPrincipal();
        String token = JWTTokenUtil.createAccessToken(selfUserEntity);
        token = JWTConfig.tokenPrefix + token;
        ResultUtil.responseJson(response, ResponseVo.success("登录成功", token));
    }
}
